package com.wonder.app.service.impl;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.service.IVtuzxKingdeeService;
import com.wonder.app.service.KingdeeOrderService;
import com.wonder.bean.kingdee.VtuzxKingdeeCloudClient;
import com.wonder.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class KingdeeOrderServiceImpl implements KingdeeOrderService {

    @Autowired
    private IVtuzxKingdeeService kingdeeService;

    @Override
    public void syncOtherIn(Long userId, String userName, Map<String, Object> header, List<Map<String, Object>> dataList) throws Exception {
        VtuzxKingdeeCloudClient client = kingdeeService.loginKingdee();
        String billNo = header.get("billNo").toString();
        String taskBillNo = header.get("taskBillNo").toString();
        VtuzxMap inStockParam = new VtuzxMap();//传单最外层
        VtuzxMap inStockModel = new VtuzxMap();
        List<Map<String, Object>> fInStockEntry = new ArrayList<>();//单据体信息
        for (Map<String,Object> material : dataList){
            String materialNo = VtuzxUtil.getString(material, "materialNo");

            String stockNo = VtuzxUtil.getString(material, "stockNo");
            String unit = VtuzxUtil.getString(material, "unit");
            double count = Math.abs(VtuzxUtil.getDouble(material, "count"));
            Boolean isFlot = VtuzxUtil.getBoolean(material,"isFlot");//是否启用批次
            Boolean isProduce = VtuzxUtil.getBoolean(material,"isProduce");//是否启用保质期

            Map<String, Object> detailMap = new LinkedHashMap<>();
            detailMap.put("FUnitID", new VtuzxMap("FNumber", unit));
            detailMap.put("FMATERIALID", new VtuzxMap("FNumber", materialNo));
            detailMap.put("FSTOCKID", new VtuzxMap("FNumber", stockNo));
            detailMap.put("FOWNERID", new VtuzxMap("FNumber", "100"));
            detailMap.put("FBASEQTY", new VtuzxMap("FNumber", unit));
            detailMap.put("FQty", count);
            if(isFlot){
                String batchNo = VtuzxUtil.getString(material, "flot");
                detailMap.put("FLOT", new VtuzxMap("FNumber", batchNo));
            }
            if (isProduce){
                String produceDate = VtuzxUtil.getString(material,"produceDate");//生产日期
                String expiryDate = VtuzxUtil.getString(material,"expiryDate");//有效期至
                detailMap.put("FPRODUCEDATE", produceDate);
                detailMap.put("FEXPIRYDATE", expiryDate);
            }
            fInStockEntry.add(detailMap);
        }
        inStockModel.append("FEntity", fInStockEntry)
                .append("FBillTypeID", new VtuzxMap("FNumber", "QTSCRKD"))
                .append("FStockOrgId", new VtuzxMap("FNumber", "100"))
                .append("FStockDirect", "GENERAL")
                .append("FDEPTID", new VtuzxMap("FNumber", "005"))
                .append("FOwnerTypeIdHead", "BD_OwnerOrg")
                .append("F_DLMH_ZTDJH", billNo)//中台单据号
                .append("F_DLMH_SCDDH", taskBillNo);//生产订单号

        inStockParam.append("model", inStockModel);

        try {
            List<VtuzxMap> stkInStockList = kingdeeService.save(client, "STK_MISCELLANEOUS", inStockParam);
            VtuzxMap stkInStockMap = stkInStockList.get(0);
            String number = VtuzxUtil.getString(stkInStockMap, "Number");
            kingdeeService.submit(client, "STK_MISCELLANEOUS", "0", number);
            kingdeeService.audit(client, "STK_MISCELLANEOUS", "0", number, "STK_InvCheckResult");
        } catch (Exception e){
            System.out.println(e.getMessage());
        }


    }

    @Override
    public void syncOtherOut(Long userId, String userName, Map<String, Object> header, List<Map<String, Object>> dataList) throws Exception {
        VtuzxKingdeeCloudClient client = kingdeeService.loginKingdee();

        String billNo = VtuzxUtil.getString(header,"billNo");
        String taskBillNo = VtuzxUtil.getString(header,"taskBillNo");
        VtuzxMap outStockParam = new VtuzxMap();
        VtuzxMap outStockModel = new VtuzxMap();
        List<Map<String, Object>> fOutStockEntry = new ArrayList<>();

        for (Map<String, Object> material: dataList) {
            String materialNo = VtuzxUtil.getString(material, "materialNo");
            String stockNo = VtuzxUtil.getString(material, "stockNo");
            String unit = VtuzxUtil.getString(material, "unit");
            double count = Math.abs(VtuzxUtil.getDouble(material, "count"));
            Boolean isFlot = VtuzxUtil.getBoolean(material,"isFlot");//是否启用批次
            Boolean isProduce = VtuzxUtil.getBoolean(material,"isProduce");//是否启用保质期


            Map<String, Object> detailMap = new LinkedHashMap<>();
            detailMap.put("FMaterialId", new VtuzxMap("FNumber", materialNo));
            detailMap.put("FUnitID", new VtuzxMap("FNumber", unit));
            detailMap.put("FBaseUnitId", new VtuzxMap("FNumber", unit));
            detailMap.put("FStockId", new VtuzxMap("FNumber", stockNo));

            detailMap.put("FOwnerTypeId", "BD_OwnerOrg");
            detailMap.put("FOwnerId", new VtuzxMap("FNumber", "100"));
            detailMap.put("FStockStatusId", new VtuzxMap("FNumber", "KCZT01_SYS"));
            detailMap.put("FKeeperId", new VtuzxMap("FNumber", "100"));
            detailMap.put("FKeeperTypeId", "BD_KeeperOrg");
            detailMap.put("FQty", count);
            if(isFlot){
                String batchNo = VtuzxUtil.getString(material, "flot");
                detailMap.put("FLOT", new VtuzxMap("FNumber", batchNo));
            }
            if (isProduce){
                String produceDate = VtuzxUtil.getString(material,"produceDate");//生产日期
                String expiryDate = VtuzxUtil.getString(material,"expiryDate");//有效期至
                detailMap.put("FPRODUCEDATE", produceDate);
                detailMap.put("FEXPIRYDATE", expiryDate);
            }
            fOutStockEntry.add(detailMap);
        }

        outStockModel.append("FEntity", fOutStockEntry)
                .append("FBillTypeID", new VtuzxMap("FNumber", "QTCKD01_SYS"))
                .append("FStockOrgId", new VtuzxMap("FNumber", "100"))
                .append("FPickOrgId", new VtuzxMap("FNumber", "100"))
                .append("FStockDirect", "GENERAL")
                .append("FOwnerTypeIdHead", "BD_OwnerOrg")
                .append("FOwnerIdHead", new VtuzxMap("FNumber", "100"))
                .append("FBaseCurrId", new VtuzxMap("FNumber", "PRE001"))
                .append("FDeptId", new VtuzxMap("FNumber", "005"))//领料部门
                .append("F_DLMH_Assistant",new VtuzxMap("FNumber", "02"))
                .append("F_DLMH_ZTDJH", billNo)//中台单据号
                .append("F_DLMH_SCDDH", taskBillNo);//生产订单号

        outStockParam.append("model", outStockModel);

       try {
           List<VtuzxMap> stkOutStockList = kingdeeService.save(client, "STK_MisDelivery", outStockParam);
           VtuzxMap stkOutStockMap = stkOutStockList.get(0);
           String number = VtuzxUtil.getString(stkOutStockMap, "Number");
           kingdeeService.submit(client, "STK_MisDelivery", "0", number);
           kingdeeService.audit(client, "STK_MisDelivery", "0", number, "STK_InvCheckResult");
       } catch (Exception e){
           throw new CustomException(e.getMessage());
       }
    }
}
